package com.app.opengllearnmodule

import android.content.res.AssetManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.opengl.GLSurfaceView
import android.os.Bundle
import android.os.Environment
import android.util.Log
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.alibaba.android.arouter.facade.annotation.Route
import java.io.File
import java.io.FileOutputStream
import java.nio.ByteBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10
import java.io.InputStream


/**
 *  Create by zhouwd on 2020/03/05
 *  Describe:
 */

private val TAG = OpenglLearnActivity::class.java.simpleName

@Route(path = "/opengl_learn_module/opengl_learn_activity")
class OpenglLearnActivity : AppCompatActivity() {

    private var activityOpenglLearnGlsv: GLSurfaceView? = null
    private var activityOpenglLearnBtn1: Button? = null

    init {
        try {
            System.loadLibrary("opengllearn")
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_opengl_learn)

        activityOpenglLearnGlsv = findViewById(R.id.activity_opengl_learn_glsv)
        activityOpenglLearnBtn1 = findViewById(R.id.activity_opengl_learn_btn1)
        activityOpenglLearnGlsv?.setEGLContextClientVersion(3)
        activityOpenglLearnGlsv?.setRenderer(render)
        /*渲染方式，RENDERMODE_WHEN_DIRTY表示被动渲染，只有在调用requestRender或者onResume等方法时才会进行渲染。RENDERMODE_CONTINUOUSLY表示持续渲染*/
        activityOpenglLearnGlsv?.renderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY


        val is123 = assets.open("aaa.jpg")
        val options = BitmapFactory.Options()
        options.inSampleSize = 2
        val bitmap: Bitmap = BitmapFactory.decodeStream(is123)
        //Log.i(TAG, "bitmap --> $bitmap")
        //val bitmap: Bitmap = readBitmapFromInputStream(is123, 500, 500, 200, 200)
        Log.i(TAG, "bitmap1 --> $bitmap")

        val data = ByteArray(bitmap.byteCount)
        val buffer: ByteBuffer = ByteBuffer.wrap(data)

        bitmap.copyPixelsToBuffer(buffer)

        glAddTexturePic(data, bitmap.byteCount, bitmap.width, bitmap.height)

        activityOpenglLearnBtn1?.setOnClickListener {

            glChange()

        }

    }



    /**
     * 获取缩放后的本地图片
     *
     * @param ins    输入流
     * @param width  宽
     * @param height 高
     * @return
     */
    private fun readBitmapFromInputStream(ins: InputStream, srcWidth: Int, srcHeight: Int, width: Int, height: Int): Bitmap {
        val options = BitmapFactory.Options()
//        options.inJustDecodeBounds = true
//        BitmapFactory.decodeStream(ins, null, options)
//        val srcWidth = options.outWidth.toFloat()
//        val srcHeight = options.outHeight.toFloat()
//        Log.i(TAG, "readBitmapFromInputStream --> srcWidth -> $srcWidth --> srcHeight -> $srcHeight")
        var inSampleSize = 1

        if (srcHeight > height || srcWidth > width) {
            inSampleSize = if (srcWidth > srcHeight) {
                (srcHeight / height)
            } else {
                (srcWidth / width)
            }
        }
//        Log.i(TAG, "inSampleSize --> $inSampleSize")
//        options.inJustDecodeBounds = false
        options.inSampleSize = inSampleSize

        return BitmapFactory.decodeStream(ins, null, options)!!
    }

    private val render = object: GLSurfaceView.Renderer {

        override fun onDrawFrame(gl: GL10?) {
            Log.i(TAG, "onDrawFrame")
            //NoiseSuppressor
            glStep()
        }

        override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {
            Log.i(TAG, "onSurfaceChanged width -> $width -> height -> $height")
            glResize(width, height)

        }

        override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
            Log.i(TAG, "onSurfaceCreated")
            glInit(assets)
        }

    }

    /**
     * 初始化
     */
    private external fun glInit(assetManager: AssetManager): Int

    /**
     * 改变尺寸
     */
    private external fun glResize(width: Int, height: Int): Int

    /**
     * 刷新
     */
    private external fun glStep(): Int

    /**
     * 添加图片纹理
     */
    private external fun glAddTexturePic(pic: ByteArray, size: Int, width: Int, height: Int): Int


    private external fun glChange(): Int

}